<template>
  <div class="job">
    <div>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="ID">
          <el-input v-model="formInline.user" placeholder="Approved by" />
        </el-form-item>
        <el-form-item label="Management name">
          <el-select v-model="formInline.region" placeholder="Activity zone">
            <el-option label="Zone one" value="shanghai" />
            <el-option label="Zone two" value="beijing" />
          </el-select>
        </el-form-item>
        <el-form-item label="Approved by">
          <el-input v-model="formInline.user" placeholder="Approved by" />
        </el-form-item>

        <el-form-item class="buttom">
          <el-button
            type="primary"
            @click="onSubmit"
            class="text-white bg-blue-300 border-transparent"
            >Query</el-button
          >
          <el-button type="" @click="onRest" class="text-blue-500"
            >Rest</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-button
        type="primary"
        class="text-white bg-blue-300 border-transparent"
        @click="dialogFormVisible = true"
        style="min-width: 80px; margin-bottom: 26px"
        >Edit</el-button
      >
    </div>

    <div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="date" label="Date" />
        <el-table-column prop="name" label="Name" />
        <el-table-column prop="state" label="State" />
        <el-table-column prop="city" label="City" />
        <el-table-column prop="address" label="Address" width="223" />
        <el-table-column prop="zip" label="Zip" />
        <el-table-column fixed="right" label="Operations">
          <template #default="scope">
            <el-button
              link
              type="primary"
              size="small"
              @click="handleClick(scope.$index)"
              >Detail</el-button
            >
            <el-button
              link
              type="primary"
              size="small"
              @click="handelEdit(scope.row)"
              >Edit</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="job-pagination">
        <div style="position: absolute; right: 0">
          <el-pagination
            style="width: 100%; text-align: right"
            v-model:currentPage="pager.currentPage4"
            v-model:page-size="pager.pageSize4"
            :page-sizes="[10, 20, 30, 40]"
            :small="pager.small"
            :disabled="pager.disabled"
            :background="pager.background"
            layout="total, sizes, prev, pager, next, jumper"
            :total="tableData.length"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>

    <!-- 添加信息人弹框 -->
    <el-dialog
      v-model="dialogFormVisible"
      title="Shipping address"
      width="40%"
      draggable
    >
      <el-form :model="form">
        <el-form-item label="Date" :label-width="formLabelWidth">
          <div class="demo-date-picker">
            <el-date-picker
              v-model="form.date"
              type="date"
              placeholder="Pick a day"
              :size="size"
              format="YYYY/MM/DD"
              value-format="YYYY-MM-DD"
            />
          </div>
        </el-form-item>
        <el-form-item label="Name" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="State" :label-width="formLabelWidth">
          <el-input v-model="form.state" autocomplete="off" />
        </el-form-item>
        <el-form-item label="City" :label-width="formLabelWidth">
          <el-input v-model="form.city" autocomplete="off" />
        </el-form-item>
        <el-form-item label="Address" :label-width="formLabelWidth">
          <el-input v-model="form.address" autocomplete="off" />
        </el-form-item>
        <el-form-item label="Zip" :label-width="formLabelWidth">
          <el-input v-model="form.zip" autocomplete="off" />
        </el-form-item>
        <el-form-item label="Tag" :label-width="formLabelWidth">
          <el-input v-model="form.Tags" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">Cancel</el-button>
          <el-button
            class="text-white bg-blue-300 border-transparent"
            @click="Confirm"
            >Confirm</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
const formInline = reactive({
  user: '',
  region: '',
})
const dialogFormVisible = ref(false)
const formLabelWidth = '100px'

const pager = reactive({
  currentPage: 1,
  pageSize: 10,
  pageSizes: [10, 20, 30, 40],
  total: 100,
  layout: 'total, sizes, prev, pager, next, jumper',
  background: true,
  small: true,
  disabled: false,
})

let form = reactive({
  date: '',
  name: '',
  state: '',
  city: '',
  address: '',
  zip: '',
  Tags: '',
})

const handleSizeChange = (val: number) => {
  pager.pageSize = val
}

const handleCurrentChange = (val: number) => {
  pager.currentPage = val
}

const onSubmit = () => {
  console.log('submit!')
}
const handleClick = (index: number) => {
  tableData.value.splice(index, 1)
}
const Confirm = () => {
  dialogFormVisible.value = false
  tableData.value.push({
    date: form.date || '',
    name: form.name || '',
    state: form.state || '',
    city: form.city || '',
    address: form.address || '',
    zip: form.zip || '',
    tags: form.Tags || '',
  })
}

const handelEdit = (row: any) => {
  form.date = row.date
  form.name = row.name
  form.state = row.state
  form.city = row.city
  form.address = row.address
  form.zip = row.zip
  dialogFormVisible.value = true
}

let tableData = ref([
  {
    date: '2016-05-03',
    name: 'Tom',
    state: 'California',
    city: 'Los Angeles',
    address: 'No. 189, Grove St, Los Angeles',
    zip: 'CA 90036',
    tags: 'Home',
  },
  {
    date: '2016-05-02',
    name: 'Tom',
    state: 'California',
    city: 'Los Angeles',
    address: 'No. 189, Grove St, Los Angeles',
    zip: 'CA 90036',
    tags: 'Office',
  },
  {
    date: '2016-05-04',
    name: 'Tom',
    state: 'California',
    city: 'Los Angeles',
    address: 'No. 189, Grove St, Los Angeles',
    zip: 'CA 90036',
    tags: 'Home',
  },
  {
    date: '2016-05-01',
    name: 'Tom',
    state: 'California',
    city: 'Los Angeles',
    address: 'No. 189, Grove St, Los Angeles',
    zip: 'CA 90036',
    tags: 'Office',
  },
  {
    date: '2016-05-02',
    name: 'Tom',
    state: 'California',
    city: 'Los Angeles',
    address: 'No. 189, Grove St, Los Angeles',
    zip: 'CA 90036',
    tags: 'Office',
  },
  {
    date: '2016-05-04',
    name: 'Tom',
    state: 'California',
    city: 'Los Angeles',
    address: 'No. 189, Grove St, Los Angeles',
    zip: 'CA 90036',
    tags: 'Home',
  },
])
</script>

<style lang="scss">
.el-select-dropdown {
  min-width: 220px !important;
}
.el-dialog__body {
  .el-input {
    width: 220px !important;
  }
}

.job {
  margin-top: 10px;
  background: white;
  padding: 30px 0 0 10px;
  width: 99.3%;
  min-height: 100vh;
  border-radius: 5px;
  .job-pagination {
    position: relative;
    margin-top: 10px;
    width: 100%;
    height: 50px;
  }

  .demo-form-inline {
    width: 100%;
  }
  .el-form-item__content {
    width: 240px;
  }
  .buttom {
    margin-right: 0;
    .el-form-item__content {
      width: 180px;
      .el-button {
        min-width: 80px;
      }
    }
  }
}
</style>
